<template>
	<section id="addvip">
		<el-row style="padding:20px;">
			<el-input placeholder="请输入会员名称" v-model="vipname">
				<template slot="prepend">会员名称</template>
			</el-input>
			<el-input placeholder="请输入会员等级。如1，表示1级会员(纯数字)" v-model="vipgrade" class="mt">
				<template slot="prepend">会员等级</template>
			</el-input>
			<el-input placeholder="请输入优惠折扣。如1，表示10折(无优惠)" v-model="goodsdiscount" class="mt">
				<template slot="prepend">产品折扣</template>
			</el-input>
			<el-input placeholder="请输入优惠折扣。如1，表示10折(无优惠)" v-model="subjectdiscount" class="mt">
				<template slot="prepend">项目折扣</template>
			</el-input>
			<el-input type="textarea" placeholder="请输入会员等级描述，方便查看" v-model="remarks" class="mt">
			</el-input>
			<el-row style="margin-top:20px;">
				<el-col :span="12">
					<el-button @click.native="close" style="width:90%;display:block;">取消</el-button>
				</el-col>
				<el-col :span="12">
					<el-button type="success" @click.native="callSubmit" style="width:90%;display:block;" :loading="loading">确定</el-button>
				</el-col>
			</el-row>
		</el-row>
	</section>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'addVipgrade',
  data () {
    return {
      vipname: '',
      vipgrade: '',
      goodsdiscount: '',
      subjectdiscount: '',
      remarks: '',
      id: '',
      loading: false
    }
  },
  mounted () {
    let RequestUrl = C.getDatas()
    if (RequestUrl.row) {
      let row = RequestUrl.row
      this.id = row.id
      this.vipname = row.vipname
      this.vipgrade = row.vipgrade
      this.goodsdiscount = row.goodsdiscount
      this.subjectdiscount = row.subjectdiscount
      this.remarks = row.remarks
    }
  },
  methods: {
    async callSubmit (index) {
      if (this.vipname === '') {
        C.error('会员名称不能为空！')
        return false
      } else if (!/^[1-9]+$/.test(this.vipgrade) || this.vipgrade < 2) {
        C.error('会员等级不正确')
        return false
      } else if (!/^[0]{1}\.(\d){1,2}$/.test(this.goodsdiscount)) {
        C.error('产品折扣不正确，如：0.95，表示9.5折')
        return false
      } else if (!/^[0]{1}\.\d{1,2}$/.test(this.subjectdiscount)) {
        C.error('项目折扣不正确，如：0.95，表示9.5折')
        return false
      }
      let data = {
        id: this.id,
        vipname: this.vipname,
        vipgrade: this.vipgrade,
        goodsdiscount: this.goodsdiscount,
        subjectdiscount: this.subjectdiscount,
        remarks: this.remarks
      }
      this.loading = true
      let res = await C.ajax('/organization/vipgrade/addvipgrade', data, 'post')
      if (res) {
        if (res.errno === 0) {
          this.loading = false
          this.$notify({
            title: '成功',
            message: res.data,
            type: 'success'
          })
          C.closeBrowserWindow(res)
        } else {
          this.loading = false
          this.$notify.error({
            title: '错误',
            message: res.errmsg
          })
        }
      }
    },
    close () {
      C.closeBrowserWindow()
    }
  }
}
</script>
<style>
.mt {
  margin-top: 15px;
}
.el-textarea__inner {
  resize: none;
}
</style>